from typing import Dict, List

from conf.config import MYSQL_CONFIG
from db.mysql_db import MysqlClient


class BuildingDAO(object):
    """ 楼盘访问对象 """
    def __init__(self, *args, **kwargs):
        super(BuildingDAO, self).__init__(*args, **kwargs)

    @staticmethod
    def get_mysql_conn():
        """ 获取mysql连接 """
        mysql_conn = MysqlClient(MYSQL_CONFIG)
        return mysql_conn

    def get_building_info(self, building_id: int) -> Dict:
        """
        获取楼盘信息
        :param building_id: 楼盘id
        :return: 楼盘信息
        """
        mysql_conn = self.get_mysql_conn()
        building_info = dict()
        try:
            sql = ('SELECT b.BName, b.AveragePrice, b.BSTime, b.BFTime, b.Phone, b.Address, '
                   'b.Decoration, b.BuildType, b.SaleStatus, b.PropertyType, b.CityID AS city_id, '
                   'i.BuildArea, i.TotalArea, i.BulkPercentage, i.GreePercentage, '
                   'i.rightsRemark, i.DesignHouseNum, i.TotalNum, i.MaxDistance, i.MinDstance, i.Shape, '
                   'p.PName, p.PMRemark, '
                   'p.ParkingNum, p.ParkingPrice, '
                   'd.Name AS city_name '
                   'FROM '
                   '(SELECT BID, BName, AveragePrice, BSTime, BFTime, '
                   'CONCAT(CenterPhone, "转", AgentPhone) AS Phone, '
                   'Address, Decoration, BuildType, SaleStatus, PropertyType, CityID '
                   'FROM build '
                   'WHERE BID = %s) b '
                   'JOIN buildintro i ON b.BID = i.BID '
                   'JOIN property p ON b.BID = p.BID '
                   'JOIN districttable d ON b.CityID = d.DTID;')
            args = (building_id, )
            building_info = mysql_conn.fetchone(sql, args=args)
        finally:
            if "mysql_conn" in dir():  # 判断连接是否成功创建，创建了才能执行close()
                mysql_conn.close()

        return building_info

    def get_building_unit_type_list(self, building_id: int) -> List:
        """
        获取楼盘的户型列表
        :param building_id: 楼盘id
        :return:
        """
        mysql_conn = self.get_mysql_conn()

        building_unit_type_list = list()
        try:
            sql = ('SELECT HName, Room, Hall, Toilet, MaxArea, counter FROM housetype WHERE bid =%s '
                   'ORDER BY `HTID`;')
            args = (building_id, )
            count, building_unit_type_list = mysql_conn.fetchall(sql, args=args)
        finally:
            if "mysql_conn" in dir():  # 判断连接是否成功创建，创建了才能执行close()
                mysql_conn.close()

        return building_unit_type_list


def main():
    # 获取楼盘的基础信息
    # building_dao = BuildingDAO()
    # res = building_dao.get_building_info(building_id=1914)
    #
    # for key, value in res.items():
    #     print(f"{key}: {value}")

    # 获取楼盘的户型信息
    building_dao = BuildingDAO()
    ls = building_dao.get_building_unit_type_list(building_id=1914)

    for index, item in enumerate(ls):
        print(f"{index}: {item}")


if __name__ == '__main__':
    main()
